<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Symfony\Component\HttpFoundation\Response;

class AdminSessionIsValid
{
    public function handle(Request $request, Closure $next): Response
    {
        if (! auth('admin')->check()) {
            return redirect()->route('admin.login');
        }

        $admin = $request->user('admin');

        if (! $admin->isActive()) {
            return redirect()->route('admin.login');
        }

        Inertia::share([
            'auth' => [
                'name' => $admin->name,
                'avatar' => $admin->avatar ?? '/static/images/avatar.jpg',
                'email' => $admin->email,
            ],
        ]);

        return $next($request);
    }
}
